import service from './request'
import request from './request'
import cookie from './cookie'

// 获取登录token
export function getToken(query) {
  return request({
    url: 'oauth/token',
    method: 'post',
    data: query
  })
}

// 获取首页数据
export function getHomeData() {
  return request({
    url: '/mobile/homepage/json'
  })
}

// 获取瀑布流数据
export function getHomeRecommend(query) {
  return request({
    url: `mobile/goods/list/paged?pageIndex=${query.pageIndex}&pageSize=${query.pageSize}`,
    method: 'get',
    query
  })
}

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    // 请求头处理
    // 请求头中携带 token 信息
    if (cookie.getCookie('token') && config.url !== 'oauth/token') {
      config.headers.Authorization = `Bearer ${cookie.getCookie('token')}`
    }
    return config
  },
  (error) => Promise.reject(error)
)
// 响应拦截器
service.interceptors.response.use(
  (res) => {
    if (res.status === 200) {
      console.log(res.data)
      return res.data
    }
    return res
  },
  (error) => {
    return error.message
  }
)

// service.interceptors.response.use(
//   (response) => {
//     const res = response
//     // ... 响应状态处理、自定义错误信息
//     return res
//   },
//   (error) => Promise.reject(error)
// )

export default {}
